﻿using Dealin.Code;
using Dealin.Data;
using Dealin.Data.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dealin.Application.ReportManage
{
    public class DefaultApp
    {

        //public DealinDbContext dbcontext = new DealinDbContext();

        public DapperDbHelper db = new DapperDbHelper();
        public string GetDefaultJson(string orgId)
        {
            string proc = string.Format("Proc_Home '{0}'", orgId);
            // var Results = dbcontext.MultipleResults(proc)
            //.AddResult<double>()
            //.AddResult<dynamic>()
            //.AddResult<dynamic>()
            //.AddResult<dynamic>()
            //.AddResult<dynamic>()
            //.Execute();

            // return Results.ToJson();
            List<dynamic> ret = new List<dynamic>();

            var list = db.QueryMultiple(proc, null, 5, System.Data.CommandType.Text);

            //总电量 当日电量 kWh
            ret.Add(list[0][0]);

            //当日各站点 电量分布 kW 
            LineChart kW = new LineChart();
            kW.xAxis = new List<dynamic>() { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", };
            kW.legend = new List<dynamic>();
            kW.series = new List<LineSerie>();
            if (list[1] != null && list[1].Count > 0)
            {
                List<dynamic> il = list[1];
                var orgs = il.Select(o => o.OrgName).Distinct();
                kW.legend = orgs.ToList();

                foreach (string org in orgs)
                {
                    var data = from p in il
                               where p.OrgName.ToString() == org
                               select p;
                    LineSerie sr = new LineSerie()
                    {
                        type = "line",
                        name = org,
                        smooth = true,
                        data = new List<double>()
                    };

                    foreach (string time in kW.xAxis)
                    {
                        var val = data.FirstOrDefault(o => o.H.ToString() == time);

                        if (val == null) sr.data.Add(0);
                        else sr.data.Add(val.kW);
                    }
                    kW.series.Add(sr);
                }


            }
            ret.Add(kW);

            //环比 电量分布
            PieChart pie = new PieChart();
            pie.legend = new List<dynamic>();
            pie.seriesData = new List<PieData>();
            if (list[2] != null && list[2].Count > 0)
            {
                List<dynamic> il = list[2];
                var energys = il.Select(o => o.Energy).Distinct();
                pie.legend = energys.ToList();
                foreach (string en in energys)
                {
                    var val = il.FirstOrDefault(o => o.Energy.ToString() == en);
                    if (val == null) continue;
                    pie.seriesData.Add(new PieData()
                    {
                        name = val.Energy,
                        value = val.kWh
                    });
                }
            }
            ret.Add(pie);


            //正负对比图 
            PRChart pr = new PRChart();
            pr.xAxis = new List<dynamic>();
            pr.data1 = new List<double>(); //利润
            pr.data2 = new List<double>(); //收入
            pr.data3 = new List<double>(); //指出
            if (list[3] != null && list[3].Count > 0)
            {
                List<dynamic> il = list[3];
                var orgs = il.Select(o => o.OrgName).Distinct();
                pr.xAxis = orgs.ToList();
                foreach (string org in pr.xAxis)
                {


                    var data = from p in il
                               where p.OrgName.ToString() == org
                               select p;

                    if (data == null) continue;

                    double dt2 = 0, dt3 = 0;
                    var d2 = data.FirstOrDefault(o => o.FeeType.ToString() == "应收");
                    if (d2 == null) pr.data2.Add(0);
                    else
                    {
                        dt2 = d2.kWh;
                        pr.data2.Add(d2.kWh);
                    }
                    var d3 = data.FirstOrDefault(o => o.FeeType.ToString() == "应付");
                    if (d3 == null) pr.data3.Add(0);
                    else
                    {
                        dt3 = d3.kWh;
                        pr.data3.Add(d3.kWh*-1);
                    }
                    var dt1 = Math.Round(dt2 - dt3, 2);
                    pr.data1.Add(dt1);

                }

            }
            ret.Add(pr);

            if (list[4] != null && list[4].Count > 0)
            {
                ret.Add(list[4]);
            }

            return ret.ToJson();
        }
    }

   
    

    
    
}
